<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\testing\phpunit\TestCase

#use tehframework\testing\yaymockery\Mockery;

abstract class TestCase extends PHPUnit_Framework_TestCase
{
	/**
	 * @var Yay_Mockery
	 */
	protected $mockery;
	
	/**
	 * @var Yay_Expectations
	 */
	protected $expect;
	
	/**
	 * @param  string $typeHint
	 * @return Yay_MockObject
	 */
	protected function mock($typeHint)
	{
		if ($this->mockery === null)
		{
			$this->mockery = new Mockery();
			$this->mockery->checking($this->expect = Yay_Expectations::create());
		}
		
		return $this->mockery->mock($typeHint);
	}
	
	protected function setUp()
	{
		$this->mockery = null;
		$this->expect  = null;
		
		$this->beforeTest();
	}
	
	protected function tearDown()
	{
		if ($this->expect !== null)
		{
			try
			{
				$this->mockery->assertIsSatisfied();
			}
			catch (Yay_NotSatisfiedException $x)
			{
				$this->fail($x->getMessage());
			}
		}
		
		$this->afterTest();
	}
	
	protected function beforeTest()
	{}
	
	protected function afterTest()
	{}
	
	/**
	 * @param  string $className
	 * @return TestCase
	 */
	protected function requireClass($className)
	{
		$dir  = dirname(dirname(dirname(dirname(dirname(realpath(__FILE__))))));
		$dirs = array($dir . '/src/', $dir . '/test/');
		$file = '/' . str_replace(T_NS_SEPARATOR, '/', $className) . '.php';
		
		foreach ($dirs as $dir)
		{
			if (file_exists($dir . $file))
			{
				require_once $dir . $file;
			}
		}
		
		return $this;
	}
}